package com.binance.binancedemo.fragment

import android.os.Bundle
import android.support.v4.view.ViewCompat
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.binance.binancedemo.R
import com.binance.binancedemo.cell.LabelCell
import com.binance.binancedemo.cell.TickerCell
import com.binance.binancedemo.entity.Ticker
import com.binance.binancedemo.recycler.BaseRecyclerAdapter
import com.binance.binancedemo.recycler.BaseRecyclerCell
import com.binance.binancedemo.recycler.BaseRecyclerViewHolder
import com.binance.binancedemo.utils.L
import com.binance.binancedemo.view.syncscroll.ScrollManager
import com.binance.binancedemo.view.syncscroll.ScrollNotifier
import kotlinx.android.synthetic.main.f_quotation.*
import java.util.*



/**
 * Created by xiangchong on 2017/12/15.
 */
class QuotationFragment: BaseFragment() {


    companion object {
        val TYPE = "TYPE"
    }

    private lateinit var mType :String

    private var mAdapter = BaseRecyclerAdapter<BaseRecyclerCell<*,*>, BaseRecyclerViewHolder>()
    private var mLabelAdapter = BaseRecyclerAdapter<BaseRecyclerCell<*,*>, BaseRecyclerViewHolder>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         super.onCreateView(inflater, container, savedInstanceState)
         return inflater.inflate(R.layout.f_quotation, null)
    }

    override fun onFirstFrameVisible() {
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rv_ticker.adapter = mAdapter
        rv_ticker.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        rv_label.adapter = mLabelAdapter
        rv_label.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        ViewCompat.setNestedScrollingEnabled(rv_label, false)
        ViewCompat.setNestedScrollingEnabled(rv_ticker, false)
        mType = arguments.getString(TYPE)
        setupScrolling()
    }

    private fun setupScrolling() {
        var view: ScrollNotifier
        val scrollManager = ScrollManager()
        scrollManager.addScrollClient(ssv_label as ScrollNotifier)
        scrollManager.addScrollClient(ssv_content as ScrollNotifier)
    }

    fun refreshCells(list: List<Ticker>?) {
        var tickers = list?.filter { it.symbol?.endsWith(mType)?:false}?: ArrayList<Ticker>()
        var beginTime = Calendar.getInstance().timeInMillis
        var cells = ArrayList<BaseRecyclerCell<*,*>>()
//        cells.add(TickerHeaderCell(""))
        tickers.forEach{
            cells.add(TickerCell(it))
        }

        var labelCells = ArrayList<BaseRecyclerCell<*,*>>()
//        labelCells.add(LabelHeaderCell(""))
        tickers.forEach{
            labelCells.add(LabelCell(it))
        }
        var createCellTime = Calendar.getInstance().timeInMillis
        L.i("cost" ,"create cells cost ${createCellTime - beginTime}")
        mAdapter.beginChange()
        mAdapter.set(cells)
        mAdapter.endChange(true)
        var refreshCellTime =  Calendar.getInstance().timeInMillis
        L.i("cost" ,"refreshCell cells cost ${refreshCellTime - createCellTime}")
        mLabelAdapter.beginChange()
        mLabelAdapter.set(labelCells)
        mLabelAdapter.endChange(true)
        L.i("cost" ,"refreshLabel cells cost ${Calendar.getInstance().timeInMillis - refreshCellTime}")
    }
}